﻿using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.Model;
using YouHP.BLL;

namespace YouHP.Pages.Member
{
    public class TreatmentRecordEdit : PatientBasePage
    {
        protected PatientTreatmentInfo PTInfo { get; set; }

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected void HandleCommon()
        {

        }

        /*判断剂量信息*/
        private bool CheckDosageData()
        {
            try
            {
                PTInfo.DataValue = Convert.ToDouble(RequestTools.GetFormString("dosage"));

                /*还需要判断是否介于剂量上下限之间*/
                int tId = RequestTools.GetFormInt("treatmentid", -1);
                TreatmentInfo tInfo = TreatmentBLL.Instance.GetTreatmentInfo(tId);
                if (tInfo != null)
                {
                    string[] limitValues = tInfo.LimitValues.Split('@');
                    if (limitValues.Length >= 2)
                    {
                        double minValue = Convert.ToDouble(limitValues[0]);
                        double maxValue = Convert.ToDouble(limitValues[1]);
                        if (PTInfo.DataValue < minValue || PTInfo.DataValue > maxValue)
                        {
                            string msg = string.Format("剂量值必须介于{0} 和 {1} 之间", minValue, maxValue);
                            this.AddErrLine(msg);
                            return false;
                        }
                    }
                    PTInfo.Unit = tInfo.Unit;
                }
            }
            catch
            {
                this.AddErrLine("剂量值必须为整数！");
                return false;
            }

            return true;
        }

        /*检查表单字段值，并装载数据*/
        private bool CheckFormData(int state)
        {
            DateTime curDate = DateTime.Now, tmpDate;
            string msg = "";
            List<PatientTreatmentInfo> ptHistoryList = null;

            PTInfo = new PatientTreatmentInfo();
            PTInfo.TreatmentId = RequestTools.GetFormInt("treatmentid", -1);
            PTInfo.PRId = this.PRInfo.Id;

            if (state == Constants.CURRENT_USING)
            {
                try
                {
                    tmpDate = Convert.ToDateTime(RequestTools.GetFormString("startdate"));
                    if (tmpDate > curDate)
                    {
                        this.AddErrLine("开始时间不能晚于当前时间！");
                        return false;
                    }
                    PTInfo.StartDate = tmpDate;
                }
                catch
                {
                    this.AddErrLine("开始时间格式不正确");
                    return false;
                }

                /*从历史治疗信息中查询时间是否符合逻辑，并提示用户*/
                ptHistoryList = PatientBLL.Instance.GetAllUseHistoryTreatments(PTInfo.PRId, PTInfo.TreatmentId);
                foreach (PatientTreatmentInfo tmpPTInfo in ptHistoryList)
                {
                    if (tmpPTInfo.State != Constants.CURRENT_USING) /*表示结束时间已经填写的纪录*/
                    {
                        if (PTInfo.StartDate >= tmpPTInfo.StartDate && PTInfo.StartDate <= tmpPTInfo.EndDate)
                        {
                            msg = string.Format("开始时间不能在已经填写的时间段{0} 和 {1} 之间",
                                tmpPTInfo.StartDate.ToString("yyyy-MM-dd"),
                                tmpPTInfo.EndDate.ToString("yyyy-MM-dd"));
                            this.AddErrLine(msg);
                            return false;
                        }
                    }
                    else
                    {
                        if (PTInfo.StartDate >= tmpPTInfo.StartDate)
                        {
                            msg = string.Format("开始时间不能比您当前使用的开始时间{0}晚",
                                tmpPTInfo.StartDate.ToString("yyyy-MM-dd"));
                            this.AddErrLine(msg);
                            return false;
                        }
                    }
                }
            }
            else  /*对历史纪录的修改，有结束时间*/
            {
                try
                {
                    tmpDate = Convert.ToDateTime(RequestTools.GetFormString("startdate"));
                    if (tmpDate > curDate)
                    {
                        this.AddErrLine("开始时间不能晚于当前时间！");
                        return false;
                    }
                    PTInfo.StartDate = tmpDate;
                }
                catch
                {
                    this.AddErrLine("开始时间格式不正确！");
                    return false;
                }

                try
                {
                    tmpDate = Convert.ToDateTime(RequestTools.GetFormString("enddate"));
                    if (tmpDate < PTInfo.StartDate)
                    {
                        this.AddErrLine("结束时间不能比开始时间早！");
                        return false;
                    }
                    else if (tmpDate > curDate)
                    {
                        this.AddErrLine("结束时间不能比当前时间晚！");
                        return false;
                    }
                    PTInfo.EndDate = tmpDate;
                }
                catch
                {
                    this.AddErrLine("结束时间格式不正确！");
                    return false;
                }

                /*从历史治疗信息中查询时间是否符合逻辑，并提示用户*/
                ptHistoryList = PatientBLL.Instance.GetAllUseHistoryTreatments(PTInfo.PRId, PTInfo.TreatmentId);
                foreach (PatientTreatmentInfo tmpPTInfo in ptHistoryList)
                {
                    if (tmpPTInfo.State != Constants.CURRENT_USING)         /*表示结束时间已经填写的纪录*/
                    {
                        if (PTInfo.StartDate >= tmpPTInfo.StartDate && PTInfo.StartDate <= tmpPTInfo.EndDate)
                        {
                            msg = string.Format("开始时间不能在已经填写的时间段{0} 和 {1} 之间",
                                tmpPTInfo.StartDate.ToString("yyyy-MM-dd"),
                                tmpPTInfo.EndDate.ToString("yyyy-MM-dd"));
                            this.AddErrLine(msg);
                            return false;
                        }
                        if (PTInfo.EndDate >= tmpPTInfo.StartDate && PTInfo.EndDate <= tmpPTInfo.EndDate)
                        {
                            msg = string.Format("结束时间不能在已经填写的时间段{0} 和 {1} 之间",
                                tmpPTInfo.StartDate.ToString("yyyy-MM-dd"),
                                tmpPTInfo.EndDate.ToString("yyyy-MM-dd"));
                            this.AddErrLine(msg);
                            return false;
                        }
                    }
                    else
                    {
                        if (PTInfo.StartDate >= tmpPTInfo.StartDate)
                        {
                            msg = string.Format("开始时间不能比您当前使用的开始时间{0}晚",
                                tmpPTInfo.StartDate.ToString("yyyy-MM-dd"));
                            this.AddErrLine(msg);
                            return false;
                        }
                        if (PTInfo.EndDate >= tmpPTInfo.StartDate)
                        {
                            msg = string.Format("结束时间不能比您当前使用的开始时间{0}晚",
                                tmpPTInfo.StartDate.ToString("yyyy-MM-dd"));
                            this.AddErrLine(msg);
                            return false;
                        }
                    }
                }
            }

            if (!CheckDosageData()) /*检查剂量*/
            {
                return false;
            }

            PTInfo.Id = RequestTools.GetFormInt("thid", -1);
            PTInfo.UsePurpose = RequestTools.GetFormInt("usepurpose", -1);
            PTInfo.StopReason = RequestTools.GetFormInt("stopreason", -1);
            PTInfo.UpdateDate = DateTime.Now;

            return true;
        }

        protected void HandlePost()
        {
            int state = RequestTools.GetFormInt("state", -1);
            if (state < Constants.CURRENT_USING || state >= Constants.MAX_USEING_SENCE)
            {
                AddErrLine("错误的参数");
                return;
            }

            if (!CheckFormData(state))
                return;

            if (state == Constants.CURRENT_USING)
            {
                PTInfo.EndDate = DateTime.MaxValue;
            }

            try
            {
                if (PatientBLL.Instance.UpdateTreatmentRecorde(PTInfo))   //成功
                {
                    ResponseHelper.Redirect("TreatmentHistory.aspx?tid=" + PTInfo.TreatmentId);
                }
            }
            catch { }
        }
        
        protected void HandleGet()
        {
            int thId = RequestTools.GetQueryInt("thid", -1);
            if (thId > 0)
            {
                PTInfo = PatientBLL.Instance.GetPatientTreatmentById(thId);
                if (PTInfo != null)
                {
                    return; //success
                }
            }

            this.AddErrLine("无效的参数");
            return;
        }
    }


}
